package com.wcy.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.domain.SysRole;
import com.wcy.model.R;
import com.wcy.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>Title:SysRoleController.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/20 14:15</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@RestController
@RequestMapping("/roles")
@Api(tags = "角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping
    @ApiOperation(value = "条件分页查询")
    @PreAuthorize("hasAuthority('sys_role_query')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示的大小"),
            @ApiImplicitParam(name = "name",value = "角色名称")
    })
    public R<Page<SysRole>> findByPage(Page<SysRole> page,String name){
        page.addOrder(OrderItem.desc("last_update_time"));
        return R.ok(sysRoleService.findByPage(page,name));
    }

    @PostMapping
    @ApiOperation(value = "新增角色")
    @PreAuthorize("hasAuthority('sys_role_create')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRole",value = "sysRole的json数据")
    })
    public R add(@RequestBody @Validated SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        if(save){
            return R.ok("新增失败");
        }
        return R.fail("新增成功");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    @PreAuthorize("hasAuthority('sys_role_delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "要删除角色的id集合")
    })
    public R delete(@RequestBody Long[] ids){
        if(ids==null||ids.length==0){
            return R.fail("要删除的数据不能为空");
        }
        boolean remove = sysRoleService.removeByIds(Arrays.asList(ids));
        if(remove){
            return R.ok();
        }
        return R.fail("删除失败");
    }
}
